package com.cysoft.lib_app_ui.ui.opensource.data;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cysoft.lib_app_ui.R;

import java.util.List;

public class DemoFuncDataAdapter extends RecyclerView.Adapter<DemoFuncDataAdapter.ViewHolder>{
    private List<String> funcList;
    private RvItemOnClickListener itemOnClickListener;

    public void setItemOnClickListener(RvItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public DemoFuncDataAdapter(List<String> funcList){
        this.funcList = funcList;
    }
    public DemoFuncDataAdapter(List<String> funcList,RvItemOnClickListener itemOnClickListener){
        this.funcList = funcList;
        this.itemOnClickListener = itemOnClickListener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String name = funcList.get(position);
        holder.tv.setText(name);
        holder.tv.getRootView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int index = holder.getAdapterPosition();
                if(itemOnClickListener != null) {
                    itemOnClickListener.onClick(index);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return funcList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_name);
        }
    }

    public interface RvItemOnClickListener {
        void onClick(int position);
    }
}
